# CCNCore::IPC::Unspool.pm
# Copyright (C) 2006, 2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

package CCNCore::IPC::Unspool;
use strict; use warnings;
use base qw(CCN::IPC::Unspool);

use CCNCore::Home;
sub here() { CCNCore::Home::config.'/local/spool/.Unspool.running' }
sub cmd()  { CCNCore::Home::manager.'/lib/CCNCore/sbin/unspool.pl' }

sub spool   { shift->{spool} }

sub initialize()
{
    my $self = shift;

    use CCNCore::IPC::Spool;
    $self->{spool} = new CCNCore::IPC::Spool;

    my %cmds;

    # Other commands: these follow the CCN::IPC::Ticket workflows
    for my $cmd (qw(
        add-server
        add-server-completed

        create-sqlite

        cdn-downstream-high-completed

        cdn-downstream-low-configuration
        cdn-downstream-all-low-completed-configuration

        configure
        configure-remote
        commit-create-db
        commit-post-db
        
        configure-system
        configure-system-configure
        configure-system-completed
        commit-create-db-completed
        commit-post-db-completed

        stop-and-reset-slave
        stop-and-reset-slave-completed
        reset-and-start-slave
        reset-and-start-slave-completed
        complete-grants
        complete-grants-completed
        create-databases
        create-databases-completed
        drop-and-reset-master
        drop-and-reset-master-completed

        distribute-keys
        merge-keys
        send-keys

        rsync
        disable-service
        enable-service

        wait
        )
    )
    {
        my $module = $cmd;
        $module =~ s/-/_/g;
        $cmds{$cmd} = ['perl',"CCNCore::spool::${module}"];
    }

    use CCNCore::Home;
    use CCNCore::Scripting; # for $deferred_file


    $self->register_commands(
        %cmds,

        # Ticket management: ticket-completed <ticket-id> [<return-value>]
        'ticket-completed'    => ['perl', 'CCNCore::spool::ticket_completed'],

        # Manager server-only: distribute all the required keys to the servers in the system
        # distribute-keys [<server-pk>]
        'distribute-keys'   => ['perl', 'CCNCore::spool::distribute_keys'],

        # File replication;  replicate-file-once <file> <remote>
        'replicate-file-once' => ['/usr/bin/perl',
            CCNCore::Home::manager.'/lib/CCNCore/spool/replicate_file_once.pl'],

        # See Scripting.pm
        'run-deferred'  => ['/bin/sh', $CCNCore::Scripting::deferred_file],


    );
    $self->SUPER::initialize(@_);
}

1;